package com.sqfw.project.system.controller;

import java.net.URLDecoder;
import java.util.List;
import java.util.Set;

import com.sqfw.common.enums.system.SysBssSignTypeEnum;
import com.sqfw.common.utils.RsaUtils;
import com.sqfw.common.utils.StringUtils;
import com.sqfw.framework.security.LoginUser;
import com.sqfw.project.system.service.ISysUserService;
import com.sqfw.project.utils.bssSign.BssSignUtils;
import com.sqfw.project.utils.kms.KmsClientUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.tomcat.util.codec.binary.Base64;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import com.sqfw.common.constant.Constants;
import com.sqfw.common.utils.SecurityUtils;
import com.sqfw.framework.security.LoginBody;
import com.sqfw.framework.security.service.SysLoginService;
import com.sqfw.framework.security.service.SysPermissionService;
import com.sqfw.framework.web.domain.AjaxResult;
import com.sqfw.project.system.domain.SysMenu;
import com.sqfw.project.system.domain.SysUser;
import com.sqfw.project.system.service.ISysMenuService;

/**
 * 登录验证
 * 
 * @author ruoyi
 */
@Slf4j
@RestController
public class SysLoginController
{
    @Autowired
    private SysLoginService loginService;

    @Autowired
    private ISysMenuService menuService;

    @Autowired
    private SysPermissionService permissionService;

    @Autowired
    private ISysUserService userService;

    @Autowired
    private KmsClientUtils kmsClientUtils;

    @Autowired
    private BssSignUtils bssSignUtils;

    /**
     * 登录方法
     * @param loginBody 登录信息
     * @return 结果
     */
    @PostMapping("/login")
    public AjaxResult login(@RequestBody LoginBody loginBody) throws Exception {
        AjaxResult ajax = AjaxResult.success();
        String decryptPassword = RsaUtils.decryptByPrivateKey(loginBody.getPassword());
        String token = loginService.login(loginBody.getUsername(), RsaUtils.decryptByPrivateKey(loginBody.getPassword()), loginBody.getCode(),
                loginBody.getUuid());
        ajax.put(Constants.TOKEN, token);
        // 验证签名
//        boolean signPassword = bssSignUtils.verifySignedData(decryptPassword, decryptPassword, SysBssSignTypeEnum.LOGIN_PASSWORD.getKey());
//        // 解密
//        Long userId = userService.getUserIdByUserName(loginBody.getUsername());
//        String data = kmsClientUtils.decryptCfb(userId.toString(), decryptPassword, SysBssSignTypeEnum.USER_PASSWORD_ENCRYPT.getKey());
//        if (!StringUtils.isEmpty(data)) {
//            if (decryptPassword.equals(data)) {
//                log.info("解密登录成功");
//            }
//        }
        return ajax;
    }

    @PostMapping("/loginByCode")
    public AjaxResult loginByCode(@RequestBody SysUser sysUser){

        AjaxResult ajax = AjaxResult.success();
        String code = sysUser.getCode();
        if (StringUtils.isEmpty(code)){
            return AjaxResult.error("工号不能为空！");
        }
        // 生成令牌
        String token = loginService.loginByCode(code);
        ajax.put(Constants.TOKEN, token);
        return ajax;

    }

    /**
     * 获取用户信息
     * 
     * @return 用户信息
     */
    @GetMapping("getInfo")
    public AjaxResult getInfo()
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        // 角色集合
        Set<String> roles = permissionService.getRolePermission(user);
        // 权限集合
        Set<String> permissions = permissionService.getMenuPermission(user);
        AjaxResult ajax = AjaxResult.success();
        ajax.put("user", user);
        ajax.put("roles", roles);
        ajax.put("permissions", permissions);
        return ajax;
    }

    /**
     * 获取路由信息
     * 
     * @return 路由信息
     */
    @GetMapping("getRouters")
    public AjaxResult getRouters()
    {
        Long userId = SecurityUtils.getUserId();
        List<SysMenu> menus = menuService.selectMenuTreeByUserId(userId);
        return AjaxResult.success(menuService.buildMenus(menus));
    }

    /**
     * 密码签名
     * */
    @PostMapping("passwordSign")
    public AjaxResult passwordSign(@RequestBody LoginBody loginBody) throws Exception {
        String signPassword = RsaUtils.decryptByPrivateKey(loginBody.getPassword());
        bssSignUtils.signData(signPassword, signPassword, SysBssSignTypeEnum.LOGIN_PASSWORD.getKey());
        return AjaxResult.success();
    }



}
